import random
from libs.base.case import *
from libs.page.wechat import Wechat
from libs.page.shenghuo import Shenghuo


class ShenghuoCase(TestCase):
    """
    爱生活小程序测试用例
    """

    def setUp(self):
        self.driver = DriverConfig().get_driver(self.deviceinfo)  # 初始化driver
        self.wx = Wechat(self.driver)  # 实例化微信 page 类
        self.sh = Shenghuo(self.driver)  # 实例化爱生活 page 类

        self.wx.login_by_wechat_number("ReminderX", "tangdou123")
        self.wx.search_miniprogram("糖豆爱生活")
        self.sh.click_tab("最新")

    def tearDown(self):
        self.driver.quit()

    @screenshot
    def test_001_put_comment(self):
        """发布评论"""
        test_comment = "哈哈哈哈  说得真好" + str(random.randint(1, 99))
        self.sh.switch_context_to_webview()
        self.sh.switch_window(self.sh.click_video)
        self.sh.switch_window(self.sh.click_comment_btn)
        # 之前的评论数
        comments_num_old = self.sh.switch_window(self.sh.get_comments_number)
        # 之前的评论内容
        self.sh.input_comment(test_comment)
        self.sh.switch_window(self.sh.click_send_btn)
        self.sh.swip_by_coordinates()
        self.wx.save_screenshot("评论截图.png")

        # 之后的评论数
        self.sh.switch_context_to_webview()
        comments_num_new = self.sh.switch_window(self.sh.get_comments_number)
        # 之后的评论数量
        comments_new = self.sh.get_comments()
        self.assertEqual(int(comments_num_old), int(comments_num_new)-1)  # 验证评论数是否+1
        self.assertEqual(test_comment, comments_new[1]["comment"])  # 验证评论内容是否正确

    @screenshot
    def atest_002_share_video(self):
        """分享视频到微信群"""
        self.sh.switch_context_to_webview()
        self.sh.switch_window(self.sh.click_video)
        self.sh.switch_window(self.sh.click_share_playing_page)
        self.sh.switch_context_to_native()
        self.wx.choose_share_to("风中的", "appium测试用例[奸笑]")

    @screenshot
    def test_003_get_topic_list(self):
        """获取频道页专辑信息"""
        self.sh.click_tab("频道")
        self.sh.switch_context_to_webview()
        self.sh.switch_window(self.sh.find_by_xpath, "//wx-view[@class='txt_box']")
        topics = self.sh.get_topic_list()
        print(topics)

if __name__ == '__main__':
    unittest.main()

